export const useWatermarkHook = () => {
  const addWatermark = (file: File): Promise<File> => {
    return new Promise((resolve, reject) => {
      const canvas = document.createElement("canvas");
      const ctx: CanvasRenderingContext2D | null = canvas.getContext("2d");
      const img = new Image();

      img.onload = () => {
        canvas.width = img.width;
        canvas.height = img.height;

        ctx!.drawImage(img, 0, 0);

        // 根据图片大小动态计算字体大小
        const fontSize = Math.max(Math.floor(canvas.width * 0.02), 12); // 字体大小至少12px
        ctx!.font = `${fontSize}px Arial`;
        ctx!.fillStyle = "rgba(255, 255, 255, 0.8)";

        const now = new Date();
        const timeStr = now.toLocaleString();

        const padding = Math.max(Math.floor(canvas.width * 0.005), 5); // 适配不同大小的图片
        const textWidth = ctx!.measureText(timeStr).width;
        const x = canvas.width - textWidth - padding;
        const y = canvas.height - padding;

        ctx!.fillText(timeStr, x, y);

        canvas.toBlob(blob => {
          resolve(new File([blob as Blob], file.name, { type: file.type }));
        }, file.type);
      };

      const reader = new FileReader();
      reader.onload = e => {
        img.src = e.target!.result as string;
      };
      reader.readAsDataURL(file);
    });
  };

  return {
    addWatermark
  };
};
